from django.conf import settings
from stark.service.s1 import StarkHandler


class LocalPermissionHandler:

    def get_add_btn(self, request, *args, **kwargs):  # 如果没有添加权限，则不显示添加按钮
        if self.has_add_btn == True:
            p_dict = request.session.get(settings.SESSION_PERMISSION_KEY)
            url_name = '%s:%s' % (self.site.namespace, self.get_add_url_name)

            if url_name in p_dict:
                return '<a class="btn btn-primary" href="%s">添加</a>' % self.btn_add_url(*args, **kwargs)

        return None

    def get_list_display(self, request, *args, **kwargs):
        p_dict = request.session.get(settings.SESSION_PERMISSION_KEY)
        edit_url_name = '%s:%s' % (self.site.namespace, self.get_edit_url_name)
        del_url_name = '%s:%s' % (self.site.namespace, self.get_delete_url_name)

        if edit_url_name not in p_dict:  # 如果没有 edit权限，则不显示 edit列
            self.list_display.remove(StarkHandler.display_edit)

        if del_url_name not in p_dict:  # 如果没有 delete权限，则不显示 delete列
            self.list_display.remove(StarkHandler.display_del)

        re_list = []
        re_list.extend(self.list_display)

        return re_list
